import Vue from 'vue'
import weixin from 'weixin-js-sdk'
import wxSDKRequest from '@/utils/wxSDKRequest'

Vue.prototype.$weixin = weixin
export const wxSdkRegister = (apiUrl, redirectUrl, jsApiList, cb) => {

  wxSDKRequest({
    url: apiUrl,
    method: 'post',
    data: {
      url: redirectUrl
    }
  }).then(({ data }) => {

    weixin.config({
      debug: false,
      appId: data.appid,
      timestamp: data.timestamp,
      nonceStr: data.nonce_str,
      signature: data.signature,
      jsApiList: jsApiList,
      openTagList: ['wx-open-launch-weapp', 'wx-open-subscribe']
    })

    weixin.ready(() => {
      // 回调函数成功的参数为true
      cb && cb(true)

      weixin.hideMenuItems({
        menuList: [
          'menuItem:copyUrl',
          'menuItem:readMode',
          'menuItem:openWithQQBrowser',
          'menuItem:openWithSafari'
        ]
      })
    })

    weixin.error((errorMsg) => {
      // 回调函数成功的参数为false
      console.error('weixin jssdk error', errorMsg)
    })
  })
}
